package com.Netty权威指南.TimeTask;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author:jml
 * @date: 2019/11/19 18:15 @description：
 */
public class TimeServer {

    public static void main(String[] args) {
		new TimeServer().bind(8080);
    }

    public void bind(int port) {
        // 配置服务端NIO线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
		            .channel(NioServerSocketChannel.class)
		            .option(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(new ChildChannelHandler());
	        ChannelFuture channelFuture = b.bind(port).sync();
	        channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
	        e.printStackTrace();
        } finally {
			bossGroup.shutdownGracefully();
	        workerGroup.shutdownGracefully();
        }
    }
	
	
	private class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
		@Override
		protected void initChannel(SocketChannel ch) throws Exception {
			ch.pipeline().addLast(new TimeServerHandler());
		}
	}
	
	
	
}
